// pages/accredit/accredit.js
const utils = require('../../utils/util.js');
const post = utils.post;
const jumpWeapp = utils.appid;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ifad: 1,
        show_info_img: '',
        show_info_url: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        // 广告切换
        let p = {
            m: 'show_info'
        };
        post(p).then(res => {
            if (res.status == 2) {
                that.setData({
                    ifad: res.status,
                    show_info_img: res.show_info_img,
                    show_info_url: res.show_info_url
                })
            }
        })
    },
    bindgetuserinfo(e) {
        let that = this;
        var _userinfo = wx.getStorageSync('_userinfo');
        var weapp_openid = wx.getStorageSync('weapp_openid');
        if (_userinfo && weapp_openid && weapp_openid == _userinfo.openId) {
            wx.navigateBack({
                delta: 1
            });
        } else {
            // 获取用户信息
            wx.login({
                success(res) {
                    var code = res.code;
                    wx.getUserInfo({
                        success(res2) {
                            var encryptedData = res2.encryptedData;
                            var iv = res2.iv;
                            var p = {
                                m: 's_get_userinfo',
                                code: code,
                                encryptedData: encryptedData,
                                iv: iv,
                                jump_weapp: jumpWeapp
                            };
                            post(p).then(res3 => {
                                if (0 == res3.r) {
                                    var userinfo = res3.userinfo;
                                    var weapp_openid = userinfo.openId;
                                    wx.setStorageSync('weapp_openid', weapp_openid); // 桔子派的openId
                                    wx.setStorageSync('_userinfo', res3.userinfo);
                                    wx.setStorageSync('device_info', wx.getSystemInfoSync());

                                    wx.showToast({
                                        title: '授权成功',
                                        icon: 'none'
                                    });
                                    wx.navigateBack({
                                        delta: 1
                                    });
                                }
                            })
                        },
                        fail(data) {
                            // console.log(data);
                            // wx.navigateBack({
                            //     delta: 2
                            // });
                        }
                    })
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    /*onShareAppMessage: function () {
  
    }*/
    submitFormId(e) {
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        var m_uid = wx.getStorageSync("weapp_uid");
        let page = getCurrentPageUrlWithArgs();
        let p = {
            m: 'colform',
            uid: m_uid,
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId,
        }
        if (page && page.length > 0) {
            p.page = page;
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        post(p).then(res => {})
    }
})